package uit.uthuoc.fragment;

import java.util.ArrayList;

import uit.uthuoc.dal.DrugDAO;
import uit.uthuoc.dal.DrugDTO;
import uit.uthuoc.dal.NameValueDTO;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.uit.uthuoc.R;

public class DrugDetailsFragment extends Fragment {
	private static final String LOG_ID = "uit.uthuoc.fragment.DrugDetailsFragment";
	private static final String EXTRA_ID = "uit.uthuoc.fragment.DrugDetailsFragment.EXTRA_ID";
	
	private DrugDTO mDrug;
	private TextView mTextView;
	private ListView mListView;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		
		if (getArguments() != null) {
			int index = getArguments().getInt(EXTRA_ID);
			mDrug = DrugDAO.getDAO().getDrug(index);
		} else {
			mDrug = new DrugDTO();
		}

	}


	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.drug_details_layout, container, false);
		
		mTextView = (TextView) v.findViewById(R.id.drug_details_tvDrugName);
		mTextView.setText(mDrug.getDrugName());
		
		mListView = (ListView) v.findViewById(R.id.drug_details_listView);
		mListView.setAdapter(new DrugDetailsAdapter(mDrug.toNameValue(getActivity())));
		
		return v;
	}


	// Create a new DrugDetailsFragment base on index, 
	// in product index should be the ID of data table.
	public static DrugDetailsFragment newInstance(int index) {
		Bundle args = new Bundle();
		args.putInt(EXTRA_ID, index);
		
		DrugDetailsFragment fragment = new DrugDetailsFragment();
		fragment.setArguments(args);
		
		return fragment;
	}
	
	private class DrugDetailsAdapter extends ArrayAdapter<NameValueDTO> {
		
		public DrugDetailsAdapter(ArrayList<NameValueDTO> list) {
			super(getActivity(), 0, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			NameValueDTO item = getItem(position);
			
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(R.layout.search_result_layout, null);
			}
			
			TextView tvName = (TextView) convertView.findViewById(R.id.search_result_tvDrugName);
			TextView tvValue = (TextView) convertView.findViewById(R.id.search_result_tvMIMSClass);
			
			tvName.setText(item.getName());
			tvValue.setText(item.getValue());
			
			// If the textView contain a link, it will be clickable
			tvValue.setMovementMethod(LinkMovementMethod.getInstance());
			
			return convertView;
		}
		
		
	}
}
